package com.cz.mes.view;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;

import java.io.File;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ColumnGen {
    public static void main(String[] args) {
        String dbGroup = "uat3";
        File viewFile = new File("C:\\Users\\root\\Desktop\\view.txt");
        List<String> lines = FileUtil.readLines(viewFile, "utf-8");
        for (String line : lines) {
            try {
                String column = line.split(",")[0];
                String tableName = column.split("\\.")[0];
                String columnName = column.split("\\.")[1];
                columnName = StrUtil.subBefore(columnName, " ", false);

                Map<String, Object> params = new HashMap<>();
                params.put("tableName", tableName);
                params.put("columnName", columnName);

                Map<String, Object> tableExistsParams = new HashMap<>();
                tableExistsParams.put("tableName", tableName);
                tableExistsParams.put("tableSchema", "mesdb");

                String tableExistsSql = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = :tableSchema AND TABLE_NAME = :tableName";
                int count = Db.use(dbGroup).queryNumber(tableExistsSql, tableExistsParams).intValue();
                if (count == 0) {
                    continue;
                }

                String sql = "SELECT DATA_TYPE,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =:tableName AND COLUMN_NAME = :columnName";
                Entity entity = Db.use(dbGroup).queryOne(sql, params);
                String dataType = entity.getStr("DATA_TYPE");
                String columnComment = entity.getStr("COLUMN_COMMENT");
                columnComment = StrUtil.subBefore(columnComment, "：", false);
                String dataIndex = StrUtil.toCamelCase(columnName);

                String virtualTableName = "production_requisition_task";

                int columnType = -1;
                if (StrUtil.contains(dataType, "varchar") || StrUtil.contains(dataType, "char")) {
                    columnType = 0;
                }
                if (StrUtil.contains(dataType, "datetime")) {
                    columnType = 2;
                }
                if (StrUtil.contains(dataType, "decimal")) {
                    columnType = 5;
                }
                if (StrUtil.contains(dataType, "int")) {
                    columnType = 4;
                }

                System.out.println(tableName + " " + columnName + " " + dataType + " " + columnComment);

                Entity vwColumn = new Entity("mes_vw_column");
                vwColumn.set("table_id", 15);
                vwColumn.set("column_title", columnComment);
                vwColumn.set("data_index", dataIndex);
                vwColumn.set("db_table_name", virtualTableName);
                vwColumn.set("db_column_name", columnName);
                vwColumn.set("width", 0);
                vwColumn.set("column_type", columnType);
                vwColumn.set("is_required",0);
                vwColumn.set("application_flag",0);
                vwColumn.set("status",1);
                vwColumn.set("create_by", 1);
                vwColumn.set("create_on", new Date());

                Db.use(dbGroup).insert(vwColumn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        //
    }
}
